package com.mofeng.myshop.controller.user;


import com.mofeng.myshop.domain.dto.SendEmailDto;
import com.mofeng.myshop.domain.dto.search.UserSearchDto;
import com.mofeng.myshop.domain.dto.user.UserDto;
import com.mofeng.myshop.service.user.UserService;
import com.mofeng.myshop.until.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户的API
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
@Tag(name = "用户相关接口", description = "用户相关接口")
public class UserController {

    private final UserService userService;


    @Operation(summary = "初始化管理员账号")
    @PostMapping("/admin_account")
    public R initAdminAccount(@RequestBody UserDto user) {
        return userService.initAdminAccount(user);
    }

    @Operation(summary = "注册")
    @PostMapping("/register")
    public R register(@RequestBody UserDto userDto) {
        return userService.register(userDto, userDto.getPhoneOrEmailCode());
    }

    @Operation(summary = "登录")
    @PostMapping("/login")
    public R login(@RequestBody UserDto user) {
        return userService.login(user);
    }

    @Operation(summary = "更新密码password 应用场景 ：忘记密码 ，修改密码")
    @PutMapping("/password")
    public R changePassword(@RequestParam("email") String email,
                            @RequestParam("newPassword") String newPassword,
                            @RequestParam("emailCode") String emailCode) {
        return userService.updatePassword(email, newPassword, emailCode);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/user_info/{user_id}")
    public R getUserInfo(@PathVariable("user_id") String userId) {
        return userService.getUserInfo(userId);
    }

    @Operation(summary = "修改邮箱")
    @PutMapping("/update_email")
    public R updateEmail(@RequestParam("email") String email,
                         @RequestParam("email_code") String emailCode) {
        return userService.updateEmail(email, emailCode);
    }

    @Operation(summary = "退出登录")
    @PostMapping("/logout")
    public R exit() {
        return userService.logout();
    }

    @Operation(summary = "获取登录用户信息")
    @GetMapping("/login-user")
    public R getLoginUser() {
        return userService.getLoginUser();
    }

    @PreAuthorize(value = "@permissionService.ADMIN()")
    @Operation(summary = "获取用户列表")
    @GetMapping("/user-list")
    public R getUserListAdmin(@RequestParam("page") int page,
                         @RequestParam("size") int size,
                         @RequestParam(value = "userKey", required = false) String userKey,
                         @RequestParam(value = "roleId", required = false) String roleId,
                         @RequestParam(value = "powerId", required = false) String powerId,
                         @RequestParam(value = "moneyMin", required = false) String moneyMin,
                         @RequestParam(value = "moneyMax", required = false) String moneyMax,
                         @RequestParam(value = "amountMin", required = false) String amountMin,
                         @RequestParam(value = "amountMax", required = false) String amountMax) {
        UserSearchDto userSearchVo = new UserSearchDto();
        userSearchVo.setKeyword(userKey);
        userSearchVo.setRoleId(roleId);
        userSearchVo.setPowerId(powerId);
        userSearchVo.setMoneyMin(moneyMin);
        userSearchVo.setMoneyMax(moneyMax);
        userSearchVo.setAmountMin(amountMin);
        userSearchVo.setAmountMax(amountMax);
        userSearchVo.setPage(page);
        userSearchVo.setSize(size);
        return userService.userListAdmin(userSearchVo);
    }

    @Operation(summary = "获取用户列表-门户")
    @GetMapping("/portal/user-list")
    public R getUserListPortal(@RequestParam("page") int page,
                         @RequestParam("size") int size,
                         @RequestParam(value = "userKey", required = false) String userKey,
                         @RequestParam(value = "roleId", required = false) String roleId,
                         @RequestParam(value = "powerId", required = false) String powerId,
                         @RequestParam(value = "moneyMin", required = false) String moneyMin,
                         @RequestParam(value = "moneyMax", required = false) String moneyMax,
                         @RequestParam(value = "amountMin", required = false) String amountMin,
                         @RequestParam(value = "amountMax", required = false) String amountMax) {
        UserSearchDto userSearchVo = new UserSearchDto();
        userSearchVo.setKeyword(userKey);
        userSearchVo.setRoleId(roleId);
        userSearchVo.setPowerId(powerId);
        userSearchVo.setMoneyMin(moneyMin);
        userSearchVo.setMoneyMax(moneyMax);
        userSearchVo.setAmountMin(amountMin);
        userSearchVo.setAmountMax(amountMax);
        userSearchVo.setPage(page);
        userSearchVo.setSize(size);
        return userService.userListPortal(userSearchVo);
    }

    @PreAuthorize(value = "@permissionService.MASTER()")
    @Operation(summary = "管理员 添加用户")
    @PostMapping("/admin/add")
    public R addUserAdmin(@RequestBody UserDto user) {
        return userService.addUserAdmin(user);
    }

    @Operation(summary = "修改用户信息")
    @PutMapping("/update")
    public R updateUser(@RequestBody UserDto user) {
        return userService.updateUser(user);
    }

    @PreAuthorize(value = "@permissionService.MASTER()")
    @Operation(summary = "管理员 删除用户")
    @DeleteMapping("/admin/del")
    public R updateUserAdmin(@RequestParam("ids") List<String> ids) {
        return userService.deleteUser(ids);
    }

    @PreAuthorize(value = "@permissionService.MASTER()")
    @Operation(summary = "管理员 初始化密码 重置用户密码")
    @PutMapping("/admin/initPassword")
    public R resetPassword(@RequestBody UserDto user) {
        return userService.resetPassword(user.getId(), user.getNewPassword());
    }

    @PreAuthorize(value = "@permissionService.ADMIN()")
    @Operation(summary = "管理员 授权用户")
    @PutMapping("/admin/empower")
    public R empower(@RequestBody UserDto user) {
        return userService.empower(user.getId());
    }

    @PreAuthorize(value = "@permissionService.ADMIN()")
    @Operation(summary = "管理员 封禁用户")
    @PutMapping("/admin/banUser")
    public R banUser(@RequestBody UserDto user) {
        return userService.banUser(user.getId());
    }


    @Operation(summary = "管理员 发送邮件")
    @PutMapping("/send-email")
    public R resetPassword(@RequestBody SendEmailDto sendEmailDto) {
        return userService.sendEmail(sendEmailDto);
    }

    @Operation(summary = "填写邀请码")
    @PutMapping("/invitation")
    public R invitation(@RequestParam("code") String invitationCode) {
        return userService.invitation(invitationCode);
    }

    @Operation(summary = "领取新人奖励")
    @GetMapping("/receive-gift/{addressId}")
    public R receiveGift(@PathVariable("addressId") String addressId) {
        return userService.receiveGift(addressId);
    }

    @Operation(summary = "获取货币排行榜")
    @GetMapping("/money-ranking/{size}")
    public R amountRanking(@PathVariable("size") int size) {
        return userService.amountRanking(size);
    }
}
